package com.szf.task.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.szf.base.dao.Page;
import com.szf.base.web.BaseActionSupport;
import com.szf.base.web.WebUtil;
import com.szf.task.entity.AppInfo;
import com.szf.task.entity.Task;
import com.szf.task.service.AppService;
import com.szf.task.service.TaskManager;
import com.szf.task.service.TaskService;
import com.szf.task.service.api.ApiInterface;
import com.szf.util.UuidGenerator;

@Namespace("/")
@Results({ @Result(name = "success", location = "/WEB-INF/jsp/task/task.jsp"),
		@Result(name = "input", location = "/WEB-INF/jsp/task/task-input.jsp") })
public class TaskAction extends BaseActionSupport {
	private Task task;
	@Autowired
	private TaskService taskService;
	@Autowired
	private TaskManager taskManager;
	@Autowired
	private AppService appService;

	@Override
	public String execute() {
		return super.execute();
	}

	public void list() {
		Page page = taskService.queryByIbatis(warpRequest(request),
				"sinlog.listTask");
		WebUtil.renderJson(response, wrapResult(page));
	}

	public void getUseAppInfo() {
		String accountId = principle.getAccountId();
		if ("1".equals(principle.getIsAdmin())) {
			accountId = "";
		}
		List<Map> list = taskService.queryUseAppInfo(accountId);
		WebUtil.renderJson(response, list);
	}

	public String add() {
		task = new Task();
		task.setTaskId(UuidGenerator.getUuid());
		task.setAccountId(principle.getAccountId());
		method = "add";
		return "input";
	}

	public String view() {
		task = taskService.findById(task.getTaskId());
		AppInfo app = appService.findById(task.getAppId());
		task.setAppName(app.getAppName());
		method = "view";
		return "input";
	}

	public void update() {
		try {
			taskService.save(task);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void delete() {
		try {
			String ids = request.getParameter("ids");
			taskService.deleteTask(ids);
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void authCallback() throws Exception {
		String code = request.getParameter("code");
		String openid = request.getParameter("openid");
		String openkey = request.getParameter("openkey");
		String taskid = request.getParameter("taskid");

		ApiInterface api = taskManager.getApi(taskid);
		Map<String, String> param = new HashMap<String, String>();
		param.put("code", code);
		param.put("openid", openid);
		param.put("openkey", openkey);

		api.authBack(param);
		WebUtil.renderText(response, "success");
	}

	public void restart() {
		JSONObject rs = new JSONObject();
		try {
			ApiInterface api = taskManager.getApi(task.getTaskId());
			if (api == null) {
				task = taskService.findById(task.getTaskId());
				taskManager.addTask(task);
				rs.put("success", true);
				rs.put("msg", "");
			} else {
				if (api.isStop()) {
					api.restart();
					rs.put("success", true);
					rs.put("msg", "");
				} else {
					rs.put("success", false);
					rs.put("msg", "任务已经在运行中");

				}
			}
			WebUtil.renderJson(response, rs);
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public void stop() {
		try {
			ApiInterface api = taskManager.getApi(task.getTaskId());
			if (api != null) {
				api.stop();
			}
			ajaxDefaultResponse();
		} catch (Exception e) {
			ajaxErrorResponse(e.getMessage());
			logger.error(e.getMessage(), e);
		}
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

}
